package yxy.game.pm2.active.command.sevenlogin;

import yxy.apple.logger.Logger;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_七日登陆;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.ActiveMessage.ActSevenLoginDrawRewardRequest;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.ArrayList;

@MessageType(proto = Command.ActSevenLoginDrawReward, classtype = ActiveMessage.ActSevenLoginDrawRewardRequest.class)
public class CMD_Active_七日登陆_DrawReward extends Command<ActSevenLoginDrawRewardRequest> {

	private static final Logger logger = Logger.create(CMD_Active_七日登陆_DrawReward.class);
	
	@Override
	public void execute(MessageSource messageSource, ActSevenLoginDrawRewardRequest request) throws Exception {
		String pcode = messageSource.getTag();
		int id = request.getId();
		ActiveMessage.ActSevenLoginDrawRewardResponse.Builder response = ActiveMessage.ActSevenLoginDrawRewardResponse.newBuilder();
		
		ActiveModule_七日登陆 am = ActiveModule_七日登陆.get(pcode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		int canget = am.cangainreward(id);
		if(canget == DrawStatus.hadget.getStatus()) {
			send(messageSource, response, Result.ActHadGet);
			return;
		}
		if(canget == DrawStatus.cannotget.getStatus()) {
			send(messageSource, response, Result.ActCannotGet);
			return;
		}
		
		ArrayList<Currency> curs = am.gainreward(id);
		if(curs == null) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		response.setId(id);
		response.setLogindays(am.getLogindays());
		
		am.dealgainreward(id);
		new CurrencyHandle().addCurrency(pcode, CCReason.活动_七日登陆, curs);
		
		for(Currency cur : curs) {
			PackageMessage.Item.Builder item = PackageMessage.Item.newBuilder();
			item.setType(cur.getType().getNumber());
			item.setDataId(cur.getDataId());
			item.setNumber(cur.getNumber());
			response.addDisplay(item.build());
			response.addUpdate(item.build());
		}
		
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActSevenLoginDrawRewardResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
	
}
